package com.guigu.www.controller.wangChenZhen;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.guigu.www.dao.bean.*;
import com.guigu.www.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class HealthAssessmentController {

    @Autowired
    TQuestionnaireService service;
    @Autowired
    TQuestionBankService bankService;
    @Autowired
    TMemberService tMemberService;
    /*体质测评*/
    @Autowired
    TBodyTypeService tBodyTypeService;
    @Autowired
    TBodyTestService tBodyTestService;
    /*心理评测*/
    @Autowired
    TMentalityResultService tMentalityResultService;
    @Autowired
    TMentalityTestService tMentalityTestService;
    /*风险评测*/
    @Autowired
    TRiskAssessmentService assessmentService;
    @Autowired
    TRiskAssessmentTestService assessmentTestService;
    /*疾病库*/
    @Autowired
    TClinicDepartmentService clinicDepartmentService;
    @Autowired
    TDiseaseLibraryService diseaseLibraryService;

    /*拿到所有疾病*/
    @GetMapping("queryDisease")
    public List<TDiseaseLibrary> queryDisease(){
        List<TDiseaseLibrary> list=diseaseLibraryService.selectList(null);
        for (TDiseaseLibrary library : list) {
            library.setClinicDepartment(clinicDepartmentService.selectById(library.getClinicDepartmentId()));
        }
        return  list;
    }


    /*提交评估测试*/
    @PostMapping("addRisk")
    public void addRisk(@RequestBody TRiskAssessment riskAssessment) {
        TRiskAssessment riskAssessmentJudge=assessmentService.selectOne(new EntityWrapper<TRiskAssessment>().eq("t_member_id",riskAssessment.getTMemberId()));
        if(riskAssessmentJudge==null){
            assessmentService.insert(riskAssessment);
            for (TRiskAssessmentTest assessmentTest : riskAssessment.getList()) {
                assessmentTest.setTRiskAssessmentId(riskAssessment.getId());
                assessmentTestService.insert(assessmentTest);
            }
        }else{
            assessmentService.update(riskAssessment,new EntityWrapper<TRiskAssessment>().eq("id",riskAssessmentJudge.getId()));
            List<TRiskAssessmentTest> list=assessmentTestService.selectList(new EntityWrapper<TRiskAssessmentTest>().eq("您日常的工作时间",riskAssessmentJudge.getId()));
            for (int i=0;i<list.size();i++){
                list.get(i).setTOption(riskAssessment.getList().get(i).getTOption());
                assessmentTestService.update(list.get(i),new EntityWrapper<TRiskAssessmentTest>().eq("id",list.get(i).getId()));
            }
        }
    }




    /*提交心理评测*/
    @PostMapping("addMentality")
    public void addMentality(@RequestBody TMentalityResult tMentalityResult){
        TMentalityResult tMentalityResultJudge=tMentalityResultService.selectOne(new EntityWrapper<TMentalityResult>().eq("t_member_id",tMentalityResult.getTMemberId()));
        if(tMentalityResultJudge==null){
            tMentalityResultService.insert(tMentalityResult);
            for (TMentalityTest tMentalityTest : tMentalityResult.getList()) {
                tMentalityTest.setTMentalityResultId(tMentalityResult.getId());
                tMentalityTestService.insert(tMentalityTest);
            }
        }else{
            tMentalityResultService.update(tMentalityResult,new EntityWrapper<TMentalityResult>().eq("id",tMentalityResultJudge.getId()));
            List<TMentalityTest> list=tMentalityTestService.selectList(new EntityWrapper<TMentalityTest>().eq("t_mentality_result_id",tMentalityResultJudge.getId()));
            for(int i=0;i<list.size();i++){
                list.get(i).setTOption(tMentalityResult.getList().get(i).getTOption());
                tMentalityTestService.update(list.get(i),new EntityWrapper<TMentalityTest>().eq("id",list.get(i).getId()));
            }
        }
    }


    /*提交体质评估*/
    @PostMapping("addBodyType")
    public void addBodyType(@RequestBody TBodyType tBodyType){
        TBodyType tBodyTypeJudge=tBodyTypeService.selectOne(new EntityWrapper<TBodyType>().eq("t_member_id",tBodyType.getTMemberId()));
        if(tBodyTypeJudge==null){
            tBodyTypeService.insert(tBodyType);
            for (TBodyTest tBodyTest : tBodyType.getList()) {
                tBodyTest.setTBodyTypeId(tBodyType.getId());
                tBodyTestService.insert(tBodyTest);
            }
        }else{
            tBodyTypeService.update(tBodyType,new EntityWrapper<TBodyType>().eq("id",tBodyTypeJudge.getId()));
            List<TBodyTest> list=tBodyTestService.selectList(new EntityWrapper<TBodyTest>().eq("t_body_type_id",tBodyTypeJudge.getId()));
            for (int i=0;i<list.size();i++) {
                list.get(i).setTOptions(tBodyType.getList().get(i).getTOptions());
                tBodyTestService.update(list.get(i),new EntityWrapper<TBodyTest>().eq("id",list.get(i).getId()));
            }
        }
    }



    /*所有问卷*/
    @GetMapping("queryTQuestionnaire")
    public List<TQuestionnaire> queryTQuestionnaire(){
        List<TQuestionnaire> list=service.selectList(null);
        for (TQuestionnaire tQuestionnaire : list)
            tQuestionnaire.setList(bankService.selectList(new EntityWrapper<TQuestionBank>().where("id IN (SELECT tikuId FROM `t_question_association` WHERE dajuanId="+tQuestionnaire.getId()+")")));
        return list;
    }

    /*拿到用户的健康评估*/
    @GetMapping("getTMemberHealth")
    public TMember getTMemberHealth(Integer id){
        TMember tMember=tMemberService.selectById(id);
        /*体质测评的结果*/
        TBodyType tBodyType=tBodyTypeService.selectOne(new EntityWrapper<TBodyType>().eq("t_member_id",id));
        if(tBodyType!=null){
            tBodyType.setList(tBodyTestService.selectList(new EntityWrapper<TBodyTest>().eq("t_body_type_id",tBodyType.getId())));
            tMember.setTBodyType(tBodyType);
        }
        /*心理评测结果*/
        TMentalityResult tMentalityResult=tMentalityResultService.selectOne(new EntityWrapper<TMentalityResult>().eq("t_member_id",id));
        if(tMentalityResult!=null){
            tMentalityResult.setList(tMentalityTestService.selectList(new EntityWrapper<TMentalityTest>().eq("t_mentality_result_id",tMentalityResult.getId())));
            tMember.setTMentalityResult(tMentalityResult);
        }
        /*风险评估的结果*/
        TRiskAssessment riskAssessment=assessmentService.selectOne(new EntityWrapper<TRiskAssessment>().eq("t_member_id",id));
        if(riskAssessment!=null){
            riskAssessment.setList(assessmentTestService.selectList(new EntityWrapper<TRiskAssessmentTest>().eq("t_risk_assessment_id",riskAssessment.getId())));
            tMember.setTRiskAssessment(riskAssessment);
        }
        return  tMember;
    }
}
